//
//  ViewController.swift
//  MockServer
//
//  Created by ethan on 16/10/30.
//  Copyright © 2016年 ethan. All rights reserved.
//

import Cocoa



class ViewController: NSViewController {
    @IBOutlet weak var splitView: NSSplitView!
    @IBOutlet weak var tableView: NSTableView!
    @IBOutlet weak var apiDetailView: ApiDetailView!
    
    @IBOutlet weak var ipTextField: NSTextField!
    @IBOutlet weak var portTextField: NSTextField!
    @IBOutlet weak var contentTypeBtn: NSPopUpButton!
    @IBOutlet weak var operateBtn: NSButton!
    @IBOutlet weak var addBtn: NSButton!
    
    @IBOutlet weak var apiMethodBtn: NSPopUpButton!
    
    

    @IBOutlet var singleTextView: NSTextView!
    let server = Server()
    var selectedRow: Int = 0
    var config = Config(port: 8088, ip: "localhost", contentType: .json, apiList: [])
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after varding the view.
        initData()
        initSubView()
    }
    
   

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


    
}

//MARK: init 

extension ViewController {
    func initSubView() {
        ipTextField.stringValue = config.ip
        portTextField.stringValue = "\(config.port)"
        contentTypeBtn.selectItem(withTitle: config.contentType.rawValue)
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.selectionHighlightStyle = .none
        
        contentTypeBtn.removeAllItems()
        contentTypeBtn.addItems(withTitles: ContentType.all)
        
        apiMethodBtn.removeAllItems()
        apiMethodBtn.addItems(withTitles: Method.all)
        
        singleTextView.delegate = self
    }
    
    func initData() {
        let a = Api(address: "/helloworld", contentType: .json, method: .get, body: "{\"msg\" : \"helloworld\"}")
        config.apiList.append(a)
        reloadDetail(api: a)
    }
}

//MARK: util

extension ViewController {
    func isReady() -> Bool {
        return true;
    }
    
    func reloadDetail(api: Api) {
        singleTextView.string = api.body
    }
}

//MARK: action

extension ViewController {
    @IBAction func responseToContentTypeBtn(_ sender: NSPopUpButton) {
        if let item = sender.selectedItem {
        
            if let method = Method(rawValue: "GET") {
                let s = "sss"
            }
            
            let cc = ContentType(rawValue: "application/json")
            if let contentType = ContentType(rawValue: "application/json") {
                config.contentType = contentType
            }
        }
    }
    
    @IBAction func responseToOperateBtn(_ sender: NSButton) {
        if (isReady()) {
            config.ip = ipTextField.stringValue
            config.port = UInt16(portTextField.stringValue)!
            
            server.start(config: config)

        } else {
            
        }
    }
    
    @IBAction func responseToAddBtn(_ sender: NSButton) {
        let new = Api.defaultApi()
        config.apiList.append(new)
        tableView.reloadData()
    }
    
    @IBAction func responseToApiMethodBtn(_ sender: NSPopUpButton) {
        if let item = sender.selectedItem {
            guard let method = Method(rawValue: item.title) else {
                return
            }
            
            config.apiList[selectedRow].method = method
        }
    }
 
}

extension ViewController: NSTableViewDataSource {
    func numberOfRows(in tableView: NSTableView) -> Int {
        return config.apiList.count
    }
    
    @objc(tableView:viewForTableColumn:row:) func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
        let identifier: String = "ApiAddress"
        let cellView = tableView.make(withIdentifier: identifier, owner: self) as! ApiTableCell
        
        cellView.api = config.apiList[row]
        cellView.row = row
        cellView.detailHandler = self
        
        return cellView
    }
    
}

extension ViewController: ApiTableCellProtocol {
    func viewDetail(api: Api, row: Int) {
        selectedRow = row
        reloadDetail(api: api)
    }
    
    func apiUpdated(api: Api, row: Int) {
        config.apiList[row] = api
    }
}

extension ViewController: NSTableViewDelegate {
    
}

extension ViewController: NSTextViewDelegate {
    func textDidChange(_ notification: Notification) {
        if let body = singleTextView.string {
            config.apiList[selectedRow].body = body
        } else {
            config.apiList[selectedRow].body = "{}"
        }
        
    }
}

